//
//  HWAssetsController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/26.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWAssetsController: HWBaseViewController {
    var topView:HWAssetsTopView!
    var tableView:UITableView!
    var tableViewProtocol = HWAssetsProtocol()
    let httpManager = HWHttpManager()
    let mutDataArr = NSMutableArray()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
        
    }
    
}

extension HWAssetsController {
    private func initUI() -> Void {
        
        navigationView.isHidden = true
        
        topView = HWAssetsTopView()
        view.addSubview(topView)
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -40
        if #available(iOS 15, *) {
            tableView.sectionHeaderTopPadding = 0
        }
    }
    
    private func initFrame() -> Void {
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
}

extension HWAssetsController {
    func loadData() -> Void {
        
        httpManager.getAssetsInfo { infoModel in
            
            self.tableView.mj_header?.endRefreshing()
            self.tableViewProtocol.assetsModel = infoModel
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.view.showHUD(title: error)
        }
        
        httpManager.getIncomeDetailsInfo { infoModel in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            if infoModel.count > 0 {
                self.mutDataArr.removeAllObjects()
                self.mutDataArr.addObjects(from: infoModel)
            }
            self.tableViewProtocol.models = self.mutDataArr as? [HWAssetsIncomeDetailModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            self.tableView.mj_footer?.endRefreshing()
            self.view.showHUD(title: error)
        }
        
//        let netGroup = DispatchGroup()
//        let workingQueue = DispatchQueue(label: "Assets")
//        
//        netGroup.enter()
//        workingQueue.async {
//            self.httpManager.getGameCircleList { models in
//                if models.count > 0 {
//                    self.modelArr.removeAllObjects()
//                    self.modelArr.addObjects(from: models)
//                }
//                netGroup.leave()
//            } failure: { error in
//                netGroup.leave()
//            }
//        }
//        
//        netGroup.enter()
//        workingQueue.async {
//            self.httpManager.getGameCircleRecommend { models in
//                netGroup.leave()
//                if models.count > 0 {
//                    self.recommendArr.removeAllObjects()
//                    self.recommendArr.addObjects(from: models)
//                }
//            } failure: { error in
//                netGroup.leave()
//            }
//        }
//        
//        netGroup.notify(queue: workingQueue) {
//            DispatchQueue.main.asyncAfter(deadline: .now()) {
//                self.collectionView.mj_header?.endRefreshing()
//                self.collectionView.mj_footer?.endRefreshing()
//                for mod in self.recommendArr {
//                    (mod as! HWGameCircleModel).isRecommend = true
//                    
//                }
//                for (index ,_) in self.modelArr.enumerated() {
//                    if index % 3 == 0 && index > 0 {
//
//                        if self.recommendArr.count > index/3 - 1 {
//                            self.modelArr.insert(self.recommendArr[index/3 - 1], at: index + index/3 - 1)
//                        }
//                    }
//                }
//                
//                self.collectionProtocol.modelArr = self.modelArr as? [HWGameCircleModel]
//                self.collectionView.reloadData()
//            }
//            
//        }
    }
    
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshBackNormalFooter()
        tableView.mj_footer?.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    @objc func loadMoreData() -> () {
        tableView.mj_footer?.endRefreshing()
        httpManager.getIncomeDetailsInfoFooter { models in
            self.tableView.mj_footer?.endRefreshing()
            if models.count > 0 {
                self.mutDataArr.addObjects(from: models)
            }
            self.tableViewProtocol.models = self.mutDataArr as? [HWAssetsIncomeDetailModel]
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_footer?.endRefreshing()
        }
        
    }
    
}
extension HWAssetsController {
    func addAction() -> Void {
       
        tableViewProtocol.promoteNoticeButtonClick = {
            let promoteNotice = HWPromotionAnnouncementViewController()
            self.navigationController?.pushViewController(promoteNotice, animated: true)
        }
        
        tableViewProtocol.promoteQRCodeButtonClick = {
            let qrCode = HWPromotionQrCodeViewController()
            self.navigationController?.pushViewController(qrCode, animated: true)
        }
        
        tableViewProtocol.divideDetailButtonClick = {
            let divideDetail = HWDividedIntoViewController()
            self.navigationController?.pushViewController(divideDetail, animated: true)
        }
        
        tableViewProtocol.didClickRow = {
            index in
//            if index.row == 2 {
//                self.navigationController?.pushViewController(HWDiscountDaiChunViewController(), animated: true)
//            }else
            if index.row == 2 {
                let myGame = HWPromoteListController()
                myGame.hideNav = false
                self.navigationController?.pushViewController(myGame, animated: true)
            }
        }
        
        tableViewProtocol.applyButtonClick = {
            self.navigationController?.pushViewController(HWApplyjiesuanViewController(), animated: true)
        }
    }
}
